import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/components/MyButton.dart';
import 'package:toys_flutter/components/RefreshableListView.dart';
import 'package:toys_flutter/pages/DynamicDetail.dart';
import 'package:toys_flutter/utils/style_utils.dart';

class MyDongTai extends StatefulWidget {
  const MyDongTai({super.key});

  @override
  State<MyDongTai> createState() => _MyDongTaiState();
}

class _MyDongTaiState extends State<MyDongTai>
    with SingleTickerProviderStateMixin {
  // 模拟数据刷新
  Future<void> _refreshData() async {
    // 模拟网络请求或数据处理
    await Future.delayed(const Duration(seconds: 2));
    // 这里可以添加你需要的刷新逻辑
    TDToast.showIconText('成功',
        icon: TDIcons.check_circle,
        direction: IconTextDirection.vertical,
        context: context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的动态'),
        bottom: TabBar(
          tabs: const [
            Tab(text: '全部动态'),
            Tab(text: '审核中'),
            Tab(text: '审核通过'),
            Tab(text: '审核驳回'),
          ],
          controller: TabController(length: 4, vsync: this),
          onTap: (index) {
            print(index);
          },
        ),
      ),
      body: RefreshableListView(
        onRefresh: _refreshData, // 绑定刷新逻辑
        children: List.generate(
            20,
            (index) => Padding(
                  padding: const EdgeInsets.all(15),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildRichText(context),
                      _buildImages(context),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          const Text('2024-7-31 12:32:14',
                              style: StyleUtils.customTextStyle),
                          MyButton(
                            title: '查看',
                            onPressed: () {
                              Get.to(const DynamicDetail(id: '345'));
                            },
                          ),
                        ],
                      ),
                      const SizedBox(height: 10),
                      const TDDivider(),
                      const SizedBox(height: 20),
                    ],
                  ),
                )),
      ),
    );
  }
}

Widget _buildImages(context) {
  // 计算图片宽度
  final double imageWidth = (MediaQuery.of(context).size.width - 30) / 3;

  return TDImage(
    assetUrl: 'images/avatar.png',
    width: imageWidth,
    height: imageWidth,
    type: TDImageType.stretch,
  );
}

Widget _buildRichText(BuildContext context) {
  return Column(
    children: [
      RichText(
        text: const TextSpan(
          text: '动态内容动态内容动态内容动态内容动态内容动态内容动态内容动态内容动态内容',
          style: StyleUtils.mainTextStyle,
        ),
      ),
      const SizedBox(height: 15)
    ],
  );
}
